package com.cuckoo.mall.thus.utils;

import android.app.Activity;
import android.support.v7.app.AlertDialog;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.bgn.baseframe.base.BaseActivity;
import com.bgn.baseframe.utils.AssetUtil;
import com.bgn.baseframe.utils.UiUtil;
import com.bgn.baseframe.utils.jsontool.GsonUtil;
import com.bigkoo.pickerview.builder.OptionsPickerBuilder;
import com.bigkoo.pickerview.listener.OnOptionsSelectListener;
import com.bigkoo.pickerview.view.OptionsPickerView;
import com.cuckoo.mall.R;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2018/4/4.
 */

public class DialogHelper {
    public static void showConfirmDialog(Activity cotext, String title, String rightText, String leftText, final DialogPressCallBack callBack) {
        AlertDialog.Builder builder = new AlertDialog.Builder(cotext);
        View view = View.inflate(cotext, R.layout.dialog_confirm, null);
        TextView tv_left = (TextView) view.findViewById(R.id.tv_cancel);
        TextView tv_right = (TextView) view.findViewById(R.id.tv_sure);
        TextView tv_title = (TextView) view.findViewById(R.id.tv_title);
        builder.setView(view);
        final AlertDialog dialog = builder.create();
        tv_title.setText(title);
        tv_left.setText(leftText);
        tv_right.setText(rightText);
        tv_left.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
            }
        });

        tv_right.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                callBack.whenPressRightButton();
                dialog.dismiss();
            }
        });
        dialog.show();
    }

    public static void showShareDialog(final Activity cotext, String title, String content, String btn, final int imageResuocre, final DialogPressCallBack callBack) {
        AlertDialog.Builder builder = new AlertDialog.Builder(cotext);
        View view = View.inflate(cotext, R.layout.dialog_share, null);

        TextView tv_right = (TextView) view.findViewById(R.id.tv_sure);
        tv_right.setText(btn);
        TextView tv_title = (TextView) view.findViewById(R.id.tv_title);
        tv_title.setText(title);
        TextView tv_content = (TextView) view.findViewById(R.id.tv_content);
        tv_content.setText(content);
        ImageView iv_image = (ImageView) view.findViewById(R.id.iv_image);
        iv_image.setImageResource(imageResuocre);
        ImageView iv_close = (ImageView) view.findViewById(R.id.iv_close);
        builder.setView(view);
        builder.setCancelable(false);
        final AlertDialog dialog = builder.create();

        iv_close.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (imageResuocre == R.mipmap.share_succeed) {

                } else {

                }
                dialog.dismiss();
            }
        });
        tv_right.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (callBack != null) {
                    callBack.whenPressRightButton();
                }
                dialog.dismiss();
            }
        });
        dialog.show();
    }

    public static OptionsPickerView showAddressPickWheel(Activity Context, final PickCallBack callBack) {
        /*读取ASSERT文件*/
        String data = AssetUtil.getJsonFromAsset("th_region3.json");

        List<Map<String, List<String>>> datas = GsonUtil.fromMapList2(data, String.class);


        final List<String> provinces = new ArrayList<>();
        final List<List<String>> citys = new ArrayList<>();
        for (int i = 0; i < datas.size(); i++) {
            Map<String, List<String>> itemMap = datas.get(i);
            provinces.addAll(itemMap.keySet());
            citys.add(itemMap.get(provinces.get(i)));
        }


        OptionsPickerView pvOptions = new OptionsPickerBuilder(Context, new OnOptionsSelectListener() {
            @Override
            public void onOptionsSelect(int options1, int option2, int options3, View v) {
                //返回的分别是三个级别的选中位置
                if (callBack != null) {
                    callBack.whenSelected(options1, option2, provinces.get(options1), citys.get(options1).get(option2));
                }
            }
        }).setSubmitText(UiUtil.getString(R.string.confirm)).setCancelText(UiUtil.getString(R.string.cancel)).build();
        pvOptions.setPicker(provinces, citys);

        pvOptions.show();
        return pvOptions;
    }

    public interface PickCallBack {
        void whenSelected(int p1, int p2, String s1, String s2);
    }

    public interface DialogPressCallBack {
        void whenPressRightButton();
    }
}
